package Servlet;

import DAO.DAOFactory;
import DAO.MaintenanceDAO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/CompleteMaintenance")
public class CompleteMaintenanceServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String maintenanceID = req.getParameter("maintenanceID");
        resp.setContentType("text/html;charset=utf-8");
        MaintenanceDAO maintenanceDAO = (MaintenanceDAO) DAOFactory.getMaintenanceDAOInstance();
        PrintWriter out = resp.getWriter();
        try {
            if (maintenanceDAO.completeMaintenance(maintenanceID)) {
                out.print("<script>alert('维修完成！');window.location.href='pendingMaintenance'</script>");
            } else {
                out.print("<script>alert('维修失败！');window.location.href='pendingMaintenance'</script>");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
